//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
  },
  onLoad:function(e){
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    that.setData({
      cid: e.id,
      namelogo: app.globalData.namelogo
    })
    console.log(that.data.cid);
  },
  getCoupon: function (e) {
    var that = this;
    var curId = e.currentTarget.dataset.id;
    that.setData({
      curId: curId
    })
    app.getSubId(that, 'applet_coupon_receive', 'toGetCoupon');
  },
  toGetCoupon: function (e) {
    var that = this;
    var id = that.data.curId;
    console.log(id);
    wx.showLoading({
      title: '领取中',
      mask: true
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_coupon_receive',
        cid: id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var couponInfo = {
            couponDetail: res.data.data.coupon,
            receiveList: res.data.data.receiveList
          };
          wx.setStorage({
            key: "couponInfo",
            data: couponInfo,
            success: function () {
              wx.redirectTo({
                url: '/subpages/useCoupon/useCoupon'
              })
            }
          })

        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var title = app.globalData.namelogo.name;
    var id = that.data.cid;
    console.log(title);
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      // path: '/subpages/shareCoupon/shareCoupon?id=' + id
      path: '/subpages/couponList/couponList?enterfrom=share'
    }
  },
})
